/*
 * Created by SharpDevelop.
 * user: Robertux
 * Date: 10/03/2008
 * Time: 21:41
 * 
 * 
 */
using System;
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;

namespace DataLayer
{
	/// <summary>
	/// Description of MyClass.
	/// </summary>
	public class BaseDatos
	{	
		MySqlConnection _conexion;
		MySqlCommand _comando;
        		
        public BaseDatos()
		{
            //if (this._conexion.State == ConnectionState.Open)
            //{
                this._conexion = new MySqlConnection("server=localhost; user id='InventadorUser'; password='InventadorPwd'; database=Inventador");
                //if (System.IO.File.Exists("inventador.db"))
                    //this._conexion.ConnectionString = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\\inventador.db;UseUTF16Encoding=True;Version=3;Pooling=True";
                this._comando = new MySqlCommand("", this._conexion);
            //}
		}
		
		public void Conectar()
		{
			if(this._conexion.State != ConnectionState.Open)
				this._conexion.Open();
		}
		
		public void Desconectar()
		{
			if(this._conexion.State != ConnectionState.Closed)
				this._conexion.Close();
		}
		
		public MySqlDataReader EjecutarConsulta(string queryString)
		{
			this._comando = new MySqlCommand(queryString, this._conexion);
			MySqlDataReader lector = this._comando.ExecuteReader();
			return lector;
		}
				
		public int EjecutarOperacion(string queryString)
		{
			this._comando = new MySqlCommand(queryString, this._conexion);
			int nResults = this._comando.ExecuteNonQuery();
			return nResults;
		}
		
		public int EjecutarEscalar(string queryString)
		{
			this._comando = new MySqlCommand(queryString, this._conexion);
			if(this._comando.ExecuteScalar() != null)
			{
				int escalar = int.Parse(this._comando.ExecuteScalar().ToString());
				return escalar;
			}
			return 0;
		}
	}
}
